package com.ruyuan.payment.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruyuan.payment.server.domain.AccountLog;
import com.ruyuan.payment.server.domain.AccountLogExample;
import com.ruyuan.payment.server.mapper.AccountLogMapper;
import com.ruyuan.payment.server.req.AccountLogQueryReq;
import com.ruyuan.payment.server.resp.AccountLogQueryResp;
import com.ruyuan.payment.server.resp.PageResp;
import com.ruyuan.payment.server.util.CopyUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AccountLogService {

    private static final Logger LOG = LoggerFactory.getLogger(AccountLogService.class);

    @Resource
    private AccountLogMapper accountLogMapper;

    public PageResp<AccountLogQueryResp> queryList(AccountLogQueryReq req) {
        Long memberId = req.getMemberId();
        AccountLogExample accountLogExample = new AccountLogExample();
        AccountLogExample.Criteria criteria = accountLogExample.createCriteria();
        if (!ObjectUtils.isEmpty(memberId)) {
            criteria.andMemberIdEqualTo(memberId);
        }
        accountLogExample.setOrderByClause("id desc");

        LOG.info("查询页码：{}", req.getPage());
        LOG.info("每页条数：{}", req.getSize());
        PageHelper.startPage(req.getPage(), req.getSize());
        List<AccountLog> accountLogList = accountLogMapper.selectByExample(accountLogExample);

        PageInfo<AccountLog> pageInfo = new PageInfo<>(accountLogList);
        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());

        List<AccountLogQueryResp> queryRespList = CopyUtil.copyList(accountLogList, AccountLogQueryResp.class);

        PageResp<AccountLogQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(queryRespList);

        return pageResp;
    }
}
